package cs.fsu.fsview;


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class LauncherActivity extends Activity{
	
	final static String TAG = "cs.fsu.fsview.LauncherActivity";
	
	private StoryAdapter[] stories;
	private String[] sections;
	private String[] sectionNames;
	//private LinearLayout feed, webLayout;
	private ListView listView;
	private ProgressDialog progress;
	private ImageButton selectedSection;
	private Thread downloadThread;
	
	
	int parseSection(int id){
		
		if(id == R.id.news)
			return 0;
		
		if(id == R.id.sports)
			return 1;
		
		if(id == R.id.arts)
			return 2;
		
		if(id == R.id.views)
			return 3;
		
		if(id == R.id.blogs)
			return 4;
		
		return -1;
	}
	
	
	public void onClick(View v){
		setAdapter(parseSection(v.getId()));
	}
	
	private void setAdapter(int storyId){
		
		final int id = storyId;
		Log.i(TAG, "Setting Adapter to " + id);
		
		// Displaying Name to Section as pop-up Toast
		if(PreferenceManager.getDefaultSharedPreferences(this).getBoolean("toast", true))
			Toast.makeText(this, sectionNames[id], Toast.LENGTH_SHORT).show();
		
		setBackgroundSelected(storyId);
		
		listView.setAdapter(stories[id]);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long viewId) {
				
				String link = stories[id].getLink(position);
				
				Log.i(TAG, "position = " + position + " link = " + link); 
				
				Intent i = new Intent(getBaseContext(), StoryActivity.class);
				i.putExtra("link","http://fsunews.com" + link);
				
				startActivity(i);
			}
		});
	}
	
	private void setBackgroundSelected(int storyId) {
		
		
		if(selectedSection != null)
			selectedSection.setBackgroundColor(Color.parseColor("#CCCC66"));
		
		switch(storyId){
		case 0:
			selectedSection = (ImageButton) findViewById(R.id.news);
			break;
			
		case 1:
			selectedSection = (ImageButton) findViewById(R.id.sports);
			break;
			
		case 2:
			selectedSection = (ImageButton) findViewById(R.id.arts);
			break;
			
		case 3:
			selectedSection = (ImageButton) findViewById(R.id.views);
			break;
			
		case 4:
			selectedSection = (ImageButton) findViewById(R.id.blogs);
			break;

		default:
			break;
		}
		
		if(selectedSection != null)
			selectedSection.setBackgroundColor(Color.parseColor("#990000"));
		
	}


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		
		//Debug.startMethodTracing("FSUnewsdump");
		
		setContentView(R.layout.main);

		//feed = (LinearLayout) findViewById(R.id.feedLayout);
		//webLayout = (LinearLayout) findViewById(R.id.webLayout);
		listView = (ListView) findViewById(R.id.previewList);
		
		sectionNames = getResources().getStringArray(R.array.SectionNames);
		
		//progress = ProgressDialog.show(this, "Getting New Stories", "From: fsunews.com", true);
		selectedSection = null;
		
		/*
		listView.setDivider(new ColorDrawable(color.black));
		listView.setDividerHeight(6);
		*/
		
		
		downloadThread = new DownloadThread(this, handler);
		downloadThread.start();
	}
	
	public void onBackPressed() {
		   Log.d(TAG, "onBackPressed Called");
		   moveTaskToBack(true);
	}
	
	private volatile Handler handler = new Handler(){
		public void handleMessage(Message msg){
			
			
			
			//progress.dismiss();
			
			// Setting Default List View
			//setAdapter(0);
		}
	};
	

	
	/* Creates the menu items */
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "About Us").setIcon(android.R.drawable.ic_menu_help);
		menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "Settings").setIcon(android.R.drawable.ic_menu_edit);
		return true;
	}
	
	public void DisplayAbout(){
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getResources().getString(R.string.about))
		       .setNegativeButton("Ok", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       }).show();
		
		//AlertDialog alert = builder.create();
		
		return;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

		case Menu.FIRST:	
			DisplayAbout();
			break;
		
		// Display Preference Activity
		case Menu.FIRST+1:
			Intent intent = new Intent();
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.setClass(getBaseContext(), PrefActivity.class);
			startActivity(intent);
			break;
			

		default:
			return false;
		}
		return true;
	}
	
}